<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
use Home\Controller\TreeController;
/**
 * 网站首页
 */
class IndexController extends HomeBaseController {

    public function home()
    {
        $this->display();
    }

    // 首页
    public function index(){
        $articles=D('Article')->getPageData();
        $assign=array(
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'cid'=>'index'
            );
        $this->assign($assign);
        $this->display();
    }

    /**
     * 更多
     */
    public function more()
    {
        $articles=D('Article')->getPageData();
        $assign=array(
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'cid'=>'index'
        );
        $this->assign($assign);
        $this->display();
    }

    public function more2()
    {
        $articles=D('Article')->getPageData();
        $assign=array(
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'cid'=>'index'
        );
        return $assign;
//        $this->assign($assign);
//        $this->display();
    }
    // 生成验证码
    public function showVerify(){
        show_verify();
    }
    /**
     * 登录
     * @param
     */
    public function login()
    {
        $user=I("post.");
        $where=array(
            'user_name'=>$user['user_name'],
            'user_pwd'=>md5($user['user_pwd'])
        );
        if(check_verify($user['verify'])){
            $userInfo=M("user")->where($where)->find();
            if($userInfo){
                session("userInfo",$userInfo);
                $this->success("登陆成功！");
            }
            else{
                $this->error("账号或密码错误！");
            }
        }
        else{
            $this->error("验证码错误！");
        }

    }
    /**
     * 注册
     * 判断用户名是否已经存在，还有用户名密码是否有了
     * @param
     */
    public function addUser()
    {
        $user=I("post.");
        $where=array(
            'user_name'=>$user['user_name']
        );
        $Info=M("user")->where($where)->find();
        if(empty($user['user_name'])||empty($user['user_pwd'])){
            $this->error("请输入用户名和密码");
        }
        else if($Info){
            $this->error("该用户已存在");
        }
        else if(check_verify($user['verify'])){
            $user['user_addtime']=time();
            $user['user_pwd']=md5($user['user_pwd']);
            $res=M("User")->add($user);
            if($res){
                $this->success("注册成功");
            }
            else{
                $this->error("注册失败@");
            }
        }
        else {
            $this->error("验证码错误");
        }
    }

    /**
     * 退出登录
     */
    public function logout(){
        session('userInfo',null);
        $this->success("退出成功");
    }

    /**
     * 提交留言
     * 每次传过来对应的pid，一级的话pid=0，二级的话pid=被回复的帖子的id
     * @param
     */
    public function addMsg()
    {
        $msg=I("post.");
        $userInfo=session("userInfo");
        if(empty($msg['about_con'])){
            $this->error("请输入内容！");
        }
        else if($userInfo==null) {
            $this->error("请先登录！");
        }
        else{
            $msg['about_addtime']=time();
            $msg['about_user']=$userInfo['user_id'];
            $res=M("about")->add($msg);
            if($res){
                $this->success("留言成功");
            }
            else{
                $this->error("留言失败");
            }
        }

    }
    /**
     * 回复页面
     * @param
     */
    public function reply()
    {
        $about_id=I("about_id");
        $this->assign("about_id",$about_id);
        $this->display();
    }

    // 分类
    public function category($cid){
        $cid=I('get.cid',0,'intval');
        // 获取分类数据
        $category=D('Category')->getDataByCid($cid);
        // 如果分类不存在；则返回404页面
        if (empty($category)) {
            header("HTTP/1.0  404  Not Found");
            $this->display('./Template/default/Home/Public/404.html');
            exit(0);
        }
        // 获取分类下的文章数据
        $articles=D('Article')->getPageData($cid);
        $assign=array(
            'category'=>$category,
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'cid'=>$cid
            );
        $this->assign($assign);
        $this->display();
    }
    /**
     * 分类更多
     */
    public function moreCate(){
        $category=D('Category')->getDataByCid(28);
        // 如果分类不存在；则返回404页面
        if (empty($category)) {
            header("HTTP/1.0  404  Not Found");
            $this->display('./Template/default/Home/Public/404.html');
            exit(0);
        }
        // 获取分类下的文章数据
        $articles=D('Article')->getPageData(28);
        $assign=array(
            'category'=>$category,
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'cid'=>28
        );
        $this->assign($assign);
        $this->display("Index/moreCate");
    }
    /**
     * 文章分类
     */
    public function ari()
    {
        $category=D('Category')->getDataByCid(28);
        // 如果分类不存在；则返回404页面
        if (empty($category)) {
            header("HTTP/1.0  404  Not Found");
            $this->display('./Template/default/Home/Public/404.html');
            exit(0);
        }
        // 获取分类下的文章数据
        $articles=D('Article')->getPageData(28);
        $assign=array(
            'category'=>$category,
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'cid'=>28
        );
        $this->assign($assign);
        $this->display("Index/category");
    }

    // 标签
    public function tag(){
        $tid=I('get.tid',0,'intval');
        // 获取标签名
        $tname=D('Tag')->getFieldByTid($tid,'tname');
        // 如果标签不存在；则返回404页面
        if (empty($tname)) {
            header("HTTP/1.0  404  Not Found");
            $this->display('./Template/default/Home/Public/404.html');
            exit(0);
        }
        // 获取文章数据
        $articles=D('Article')->getPageData('all',$tid);
        $assign=array(
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'title'=>$tname,
            'title_word'=>'拥有<span class="b-highlight">'.$tname.'</span>标签的文章',
            'cid'=>'index'
            );
        $this->assign($assign);
        $this->display();
    }

    // 文章内容
    public function article(){
        $aid=I('get.aid',0,'intval');
        $cid=intval(cookie('cid'));
        $tid=intval(cookie('tid'));
        $search_word=cookie('search_word');
        $search_word=empty($search_word) ? 0 : $search_word;
        $read=cookie('read');
        // 判断是否已经记录过aid
        if (array_key_exists($aid, $read)) {
            // 判断点击本篇文章的时间是否已经超过一天
            if ($read[$aid]-time()>=86400) {
                $read[$aid]=time();
                // 文章点击量+1
                M('Article')->where(array('aid'=>$aid))->setInc('click',1);
            }
        }else{
            $read[$aid]=time();
            // 文章点击量+1
            M('Article')->where(array('aid'=>$aid))->setInc('click',1);
        }
        cookie('read',$read,864000);
        switch(true){
            case $cid==0 && $tid==0 && $search_word==(string)0:
                $map=array();
                break;
            case $cid!=0:
                $map=array('cid'=>$cid);
                break;
            case $tid!=0:
                $map=array('tid'=>$tid);
                break;
            case $search_word!==0:
                $map=array('title'=>$search_word);
                break;
        }
        // 获取文章数据
        $article=D('Article')->getDataByAid($aid,$map);
        // 如果文章不存在；则返回404页面
        if (empty($article['current']['aid'])) {
            header("HTTP/1.0  404  Not Found");
            $this->display('./Template/default/Home/Public/404.html');
            exit(0);
        }
        // 获取评论数据
        $comment=D('Comment')->getChildData($aid);
        $assign=array(
            'article'=>$article,
            'comment'=>$comment,
            'cid'=>$article['current']['cid']
            );
        if (!empty($_SESSION['user']['id'])) {
            $assign['user_email']=M('Oauth_user')->getFieldById($_SESSION['user']['id'],'email');
        }
        $this->assign($assign);
        $this->display();
    }

    // 随言碎语
    public function chat(){
        $assign=array(
            'data'=>D('Chat')->getDataByState(0,1),
            'cid'=>'chat'
            );
        $this->assign($assign);
        $this->display();
    }

    // 开源项目
    public function git(){
        $assign=array(
            'cid'=>'git'
            );
        $this->assign($assign);
        $data=D('Zykind')->getDataByState(0,'all');
        $this->assign('data',$data);
        $ziyuan_kind=I("kind_id");
        if($ziyuan_kind){
            $where=array(
                'a.is_delete'=>0,
                'a.is_show'=>1,
                'a.ziyuan_kind'=>$ziyuan_kind
            );
        }
        else{
            $where=array(
                'a.is_delete'=>0,
                'a.is_show'=>1,
            );
        }
        $data2 = M('Ziyuan  as  a')
            ->join('bjy_Zykind  as  b  on b.kind_id = a.ziyuan_kind')
            ->where($where)
            ->select();
        $this->assign('data2',$data2);
        $this->display();
    }

    // 站内搜索
    public function search(){
        $search_word=I('get.search_word');
        $articles=D('Article')->getDataByTitle($search_word);
        $assign=array(
            'articles'=>$articles['data'],
            'page'=>$articles['page'],
            'title'=>$search_word,
            'title_word'=>'搜索到的与<span class="b-highlight">'.$search_word.'</span>相关的文章',
            'cid'=>'index'
            );
        $this->assign($assign);
        $this->display('tag');
    }

    // ajax评论文章
    public function ajax_comment(){
        $data=I('post.');
        if(empty($data['content']) || !isset($_SESSION['user']['id'])){
            die('未登录,或内容为空');
        }else{
            $cmtid=D('Comment')->addData(1);
            echo $cmtid;
        }
    }
    /**
     * 关于我们
     */
    public function about()
    {
        $res=M("about as b")
            ->join("bjy_user as a on a.user_id=b.about_user")
            ->where("b.about_status=1")
            ->select();
        $res1=TreeController::tree($res);
        $this->assign("cate",$res1);
        $this->display();
    }


}
